package io.renren.modules.offlinestudy.service.impl;

import io.renren.common.exception.RRException;
import io.renren.modules.offlinestudy.dao.SubscribedateDao;
import io.renren.modules.offlinestudy.entity.SubscribeEntity;
import io.renren.modules.offlinestudy.entity.SubscribedateEntity;
import io.renren.modules.offlinestudy.service.SubscribedateService;
import io.renren.modules.onlinestudy.entity.XcBmEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;



@Service("subscribedateService")
public class SubscribedateServiceImpl implements SubscribedateService {
	@Autowired
	private SubscribedateDao subscribedateDao;
	
	@Override
	public List<SubscribedateEntity> queryObjects(Integer id){
        return subscribedateDao.queryObjects(id);
    }
	
	@Override
	public List<SubscribedateEntity> queryList(Map<String, Object> map){
		return subscribedateDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return subscribedateDao.queryTotal(map);
	}
	
	@Override
	public void save(List<SubscribedateEntity> subscribedates){
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        String address = String.valueOf(sysUserEntity.getDeptId());
        SubscribedateEntity subscribedate = subscribedates.get(0);
        subscribedate.setAddress(address);
        Integer subscribe_count = subscribedateDao.selSubscribeBystartDate(subscribedate);
        if(subscribe_count > 0 ){
            throw new RRException("该时间段内已有其他学习计划，请重新设置学习日期！");
        }

        subscribe_count = subscribedateDao.selSubscribeByendDate(subscribedate);
        if(subscribe_count > 0){
            throw new RRException("该时间段内已有其他学习计划，请重新设置学习日期！");
        }
        String typeid = UUID.randomUUID().toString();
        subscribedates.forEach((subscribedateEntity) ->{
            subscribedateEntity.setAddress(address);
            subscribedateEntity.setTypeid(typeid);
            subscribedateDao.save(subscribedateEntity);
        });
	}
	
	@Override
	public void update(List<SubscribedateEntity> subscribedates){
        Integer subscribe_count = subscribedateDao.selSubscribeBystartDate(subscribedates.get(0));
        if(subscribe_count > 0 ){
            throw new RRException("该时间段内已有其他学习计划，请重新设置学习日期！");
        }

        subscribe_count = subscribedateDao.selSubscribeByendDate(subscribedates.get(0));
        if(subscribe_count > 0){
            throw new RRException("该时间段内已有其他学习计划，请重新设置学习日期！");
        }
        subscribedates.forEach((subscribedate)->{
            subscribedateDao.update(subscribedate);
        });
	}
	
	@Override
	public void delete(Integer id){
		subscribedateDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		subscribedateDao.deleteBatch(ids);
	}

    /**
     *               *******线下预约******************
     */
    /**
     *
     * @param id
     * @return
     */
    @Override
    public SubscribeEntity querySubcribeObject(Integer id){
        return subscribedateDao.querySubcribeObject(id);
    }

    @Override
    public List<SubscribeEntity> querySubscribeList(Map<String, Object> map){
        return subscribedateDao.querySubscribeList(map);
    }

    @Override
    public int querySubscribeTotal(Map<String, Object> map){
        return subscribedateDao.querySubscribeTotal(map);
    }

    @Override
    public void subscribeSave(SubscribeEntity subscribe){
        subscribedateDao.subscribeSave(subscribe);
    }

    @Override
    public void subscribeUpdate(SubscribeEntity subscribe){
        subscribedateDao.subscribeUpdate(subscribe);
    }

    @Override
    public void subscribeDeleteBatch(Integer[] ids){
        subscribedateDao.subscribeDeleteBatch(ids);
    }

    @Override
    public List<SubscribedateEntity> selectValidateSubscribeDate(String date, String studytype) {
        return subscribedateDao.selectValidateSubscribeDate(date, studytype);
    }

    @Override
    public List<Map<String, Object>> selSubscribeExcel(Map<String, Object> map) {
        return subscribedateDao.selSubscribeExcel(map);
    }

    @Override
    public XcBmEntity selXyxx(String userid) {
        return subscribedateDao.selXyxx(userid);
    }
}
